<template>
  <view class="page">
    <view class="row">
      <text>头像</text>
      <view class="avatar">
        <image class="avatar__img" :src="avatarUrl || defaultAvatarUrl" />
        <button class="avatar__btn" open-type="chooseAvatar" @chooseavatar="avatar" />
      </view>
    </view>
    <view class="row">
      <text>昵称</text>
      <input ref="input" v-model="nickname" @change="nicknameChange" class="nickname" type="nickname" placeholder="请输入昵称"/>
    </view>
    <view @tap="confirm" class="btn">确认</view>
  </view>
</template>

<script>
import user from '@/mixins/user.js';
import { YD_formatUrl } from '@/utils/tools.js';

const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

export default {
  mixins: [user],
  data() {
    return {
      defaultAvatarUrl,
      avatarUrl: '',
      nickname: ''
    }
  },
  onLoad() {
    if(this.userInfo.avatar) {
      this.avatarUrl = YD_formatUrl(this.userInfo.avatar);
      this.nickname = this.userInfo.nickname;
    }
  },
  methods: {
    avatar(e) {
      const { avatarUrl } = e.detail;
      this.avatarUrl = avatarUrl
    },
    nicknameChange({ detail }) {
      this.nickname = detail.value;
    },
    confirm() {
      uni.navigateBack({
        delta: 1,
        success: () => {
          uni.$emit('auth', {
            avatar: this.avatarUrl,
            nickname: this.nickname,
          });
        }
      });
    }
  }
}
</script>

<style lang="scss">
page {
  width: 100%;
  height: 100%;
}
.page {
  width: 100%;
  height: calc(100% - 20rpx);
  margin-top: 20rpx;
  background-color: #fff;
  box-sizing: border-box;
  padding: 0 24rpx;
}
.row {
  width: 100%;
  height: 88rpx;
  border-bottom: 2rpx solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 28rpx;
  color: #333;
}
.avatar {
  width: 78rpx;
  height: 78rpx;
  position: relative;
  border-radius: 50%;
  overflow: hidden;
}
.avatar__img {
  width: 100%;
  height: 100%;
}
.nickname {
  flex: 1;
  text-align: right;
  margin-left: 100rpx;
}
.avatar__btn {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
}
.btn {
  width: 94%;
  height: 80rpx;
  border-radius: 40rpx;
  background-color: $primary;
  color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  line-height: 1;
  font-size: 28rpx;
  margin-top: 62rpx;
  margin-left: auto;
  margin-right: auto;
}
</style>
